package app.server.project.cloud.random.service;

import app.server.project.cloud.random.db.dao.DeviceBaseDao;
import app.server.project.cloud.random.dto.device.*;
import app.server.project.core.constants.Def;
import app.server.project.core.exception.SysError;
import app.server.project.core.util.AssertUtil;
import app.server.project.cloud.random.db.dao.DevNameDao;
import app.server.project.cloud.random.db.dao.DevStatusDao;
import app.server.project.cloud.random.db.dao.UserDeviceDao;
import app.server.project.cloud.random.db.entity.DevNameEntity;
import app.server.project.cloud.random.db.entity.UserDeviceEntity;
import app.server.project.main.service.dto.device.*;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class UserDeviceService {

    private UserDeviceDao userDeviceDao;
    private DeviceBaseDao deviceBaseDao;
    private DevNameDao devNameDao;
    private DevStatusDao devStatusDao;

    public List<DeviceDto> getAllDevice(long userId){
        var data=this.userDeviceDao.getData(userId);
        return data.stream().map(v->{
            var names=this.devNameDao.getData(v.getDeviceId());
            var status=this.devStatusDao.getData(v.getDeviceId());
            return DeviceDto.of(DevBaseDto.of(v),
                    names.stream().map(DevChNameDto::of).toList(),
                    status.stream().map(DevStatusDto::of).toList()
                    );
        }).toList();
    }

    public void bindDevice(long userId,long deviceId){
        var devCfg=this.deviceBaseDao.getDataById(deviceId);
        AssertUtil.isTrue(devCfg.getId()>Def.Zero,SysError.Non_Dev);
        //检查是否已经绑定
        var data=this.userDeviceDao.getData(userId);
        if (data.stream().anyMatch(v->v.getDeviceId()==deviceId)){
            return;
        }
        data.add(UserDeviceEntity.of(IdUtil.getSnowflakeNextId(),userId,deviceId, devCfg.getDeviceTypeId()));
    }

    public void unBind(long userId,long deviceId){
        //检查是否已经绑定
        var data=this.userDeviceDao.getData(userId);
        data.removeIf(v->{
            if (v.getDeviceId()==deviceId){
                v.setIsDel(Def.True);
                return true;
            }
            return false;
        });
    }



    public void changeRoomId(ChangeDevRoomReq req,long userId){
        AssertUtil.isTrue(req.getRoomId()==Def.Zero, SysError.EmpTyRoomId);
        //检查是否已经绑定
        var data=this.userDeviceDao.getData(userId);
        data.forEach(v->{
            if (v.getDeviceId()==req.getDevId()){
                v.setRoomId(req.getRoomId());
            }
        });
    }

    public void updateName(ChangeNameReq req,long userId){
        AssertUtil.isTrue(StrUtil.isNotBlank(req.getName()), SysError.EmptyParam);
        //检查是否已经绑定
        var data=this.userDeviceDao.getData(userId);
        data.forEach(v->{
            if (v.getDeviceId()==req.getDevId()){
                v.setName(req.getName());
            }
        });
    }

    public void updateDevChName(ChangeDevChNameReq req){
        AssertUtil.isTrue(StrUtil.isNotBlank(req.getName()), SysError.EmptyParam);
        var data=this.devNameDao.getData(req.getDevId());
        if (data.stream().anyMatch(c->{
            return c.getDevCh()== req.getDevCh();
        })){
            data.forEach(v->{
                if (v.getDevCh()== req.getDevCh()){
                    v.setName(req.getName());
                }
            });
        }else {
            data.add(DevNameEntity.of(IdUtil.getSnowflakeNextId(), req.getDevCh(), req.getDevId(), req.getName()));
        }
    }
}
